#include "mygraphicitem.h"
#include <QPainter>
#include <QGraphicsSceneMouseEvent>
MyGraphicItem::MyGraphicItem(QGraphicsItem *parent)
    : QGraphicsItem(parent)
    , m_boundingRect(0, 0, 200, 100)
    , m_bitemRect(5, 5, 190, 90)
    , m_bSelect(false)
{
    setFlag(QGraphicsItem::ItemIsMovable, true);
    setFlag(QGraphicsItem::ItemIsSelectable, true);
    setFlag(QGraphicsItem::ItemSendsGeometryChanges, true);
    setFlag(QGraphicsItem::ItemIsFocusable, true);
}

QRectF MyGraphicItem::boundingRect() const
{
    return m_boundingRect;
}

bool MyGraphicItem::bChangeCur(QPointF point, MouseMoveDirect& direct)
{
    QPointF itempos = mapFromScene(point);
    if(m_topRect.contains(itempos))
    {
        direct = MouseMoveDirect::MouseMoveNorth;
        return true;
    }
    else if(m_lefrRect.contains(itempos))
    {
        direct = MouseMoveDirect::MouseMoveWest;
        return true;
    }
    else if(m_rightRect.contains(itempos))
    {
        direct = MouseMoveDirect::MouseMoveEast;
        return true;
    }
    else if(m_bottomRect.contains(itempos))
    {
        direct = MouseMoveDirect::MouseMoveSouth;
        return true;
    }

    return false;
}

void MyGraphicItem::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    QGraphicsItem::mousePressEvent(event);
    m_bSelect = true;
    update();
}

void MyGraphicItem::mouseReleaseEvent(QGraphicsSceneMouseEvent *event)
{
     QGraphicsItem::mouseReleaseEvent(event);
}

// void MyGraphicItem::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
// {
//     static int i = 1;
//     i++;
//     qDebug() << "mousemove: " << i;
//     QGraphicsItem::mouseMoveEvent(event);
//     if(m_topRect.contains(event->pos()))
//     {
//         int a = 9999;
//     }
// }

void MyGraphicItem::focusOutEvent(QFocusEvent *event)
{
    QGraphicsItem::focusOutEvent(event);
    m_bSelect = false;
    update();
}

void MyGraphicItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    if(m_bSelect)
    {
        painter->setRenderHint(QPainter::Antialiasing);  // 开启抗锯齿

        QPen pen(Qt::blue);
        pen.setWidth(2);  // 设置画笔宽度
        pen.setStyle(Qt::DashLine);
        painter->setPen(pen);
        painter->drawRect(m_bitemRect);
        // 计算并绘制每条边上中点处的小矩形
        QPointF topCenter = m_bitemRect.topLeft() + QPointF(m_bitemRect.width() / 2, 0);
        QPointF bottomCenter = m_bitemRect.bottomLeft() + QPointF(m_bitemRect.width() / 2, 0);
        QPointF leftCenter = m_bitemRect.topLeft() + QPointF(0, m_bitemRect.height() / 2);
        QPointF rightCenter = m_bitemRect.topRight() + QPointF(0, m_bitemRect.height() / 2);

        qreal sideLength = 10;
        qreal halfSide = sideLength / 2;

        pen.setStyle(Qt::SolidLine);
        painter->setPen(pen);
        // 在每条边的中点位置绘制小方块
        m_topRect = QRectF(topCenter.x() - halfSide, topCenter.y() - halfSide, sideLength, sideLength);
        m_rightRect = QRectF(rightCenter.x() - halfSide, rightCenter.y() - halfSide, sideLength, sideLength);
        m_bottomRect = QRectF(bottomCenter.x() - halfSide, bottomCenter.y() - halfSide, sideLength, sideLength);
        m_lefrRect = QRectF(leftCenter.x() - halfSide, leftCenter.y() - halfSide, sideLength, sideLength);

        painter->fillRect(m_topRect, Qt::white);
        painter->fillRect(m_rightRect, Qt::white);
        painter->fillRect(m_bottomRect, Qt::white);
        painter->fillRect(m_lefrRect, Qt::white);

        painter->drawRect(m_topRect);
        painter->drawRect(m_rightRect);
        painter->drawRect(m_bottomRect);
        painter->drawRect(m_lefrRect);
    }
}
